package com.doujiang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doujiang.domain.User;
import com.doujiang.mapper.UserMapper;
import com.doujiang.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户管理Service业务层处理
 *
 * @author 王伟舰
 * @date 2025-07-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据邮箱查询用户
     *
     * @param email 用户邮箱
     * @return 用户管理
     */
    @Override
    public User getUserByEmail(String email) {
        return userMapper.selectUserByEmail(email);
    }

    /**
     * 根据邮箱和密码验证用户
     *
     * @param email    用户邮箱
     * @param password 用户密码
     * @return 验证通过返回true，否则返回false
     */
    @Override
    public boolean authenticate(String email, String password) {
        User user = userMapper.selectUserByEmail(email);
        return user != null && user.getPassword().equals(password);
    }

    /**
     * 根据邮箱和密码验证用户并返回用户信息
     *
     * @param email    用户邮箱
     * @param password 用户密码
     * @return 验证通过返回用户信息，否则返回null
     */
    @Override
    public User authenticateAndGetUser(String email, String password) {
        User user = userMapper.selectUserByEmail(email);
        if (user != null && user.getPassword().equals(password)) {
            return user;
        }
        return null;
    }
}
